﻿package com.mod  {
	
	import com.calefk.core.PageManager;
	import com.calefk.core.Project;
	import com.calefk.util.FPSCounter;
	import com.calefk.util.Logger;
	import com.mod.data.Config;
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;

	public class ModProject extends Project {
		
		public static var instance:ModProject;
		public static var config:Config;
        
		public function ModProject(canvas:MovieClip) {
			super(canvas);
			
			 canvas.stage.scaleMode = StageScaleMode.NO_SCALE;
			 canvas.stage.align = StageAlign.TOP_LEFT;

			canvas.stage.addEventListener (Event.RESIZE, resizeListener);
		}
		
		private function resizeListener(e:Event):void 
		{
			Logger.echo("stageWidth: " + canvas.stage.stageWidth + " stageHeight: " + canvas.stage.stageHeight);
		}
        
        override protected function onFilesInit():void {
            // agregar archivos para poder ser utilizados cuando se invoce "onPrepareFiles",
            // usar los metodos addFileSWFInit o addFileXMLInit
        }

        override protected function onPrepareFiles():void {
            // agregar mas archivos para cargar
            addFileXML("xml/config.xml" , Global.config);  
            addFileXML("xml/strings.xml" , Global.strings);  
            addFileXML("xml/game.xml" , Global.dataGame);  
			addAssets("lib/assets.swf");
			addAssets("lib/assets_enemies.swf");
            setDefaultSounds("lib/sounds.swf"); 
        }
		
		override protected function getNewPageManager():PageManager {
			return new PMGame(mCanvas.mcCanvasProject);
		}
		
		public static function getPM():PageManager {
			return Global.project.pageManager;
		}
		
        override protected function onProgressPercent(percent:Number, total:Number):void {
           // colocar aqui un loading de arte para setear el porcentaje 
           if (mCanvas) {
                try {
                   if (mCanvas.mc_loading) {
                       if (mCanvas.mc_loading.txtPercent) {
                            mCanvas.mc_loading.txtPercent.text = int(percent);
                       }
                   }
                } catch (e:*) {}
           }
        }
        
        override protected function onEndLoad():void {
            // que hacer justo cuando termina de cargar,
            // por defecto se redirecciona el frame para inicializar la aplicacion
            onRedirectToInit();
        }
		
		override protected function onInit():void {
            // identificar los sonidos para su uso
            Global.registerSounds();
            // Init Logger
			Logger.initialize(mCanvas.canvasLogger , PW , PH , Global.config.activeLog , Global.config.activeTrace);
			// First Page
			mPageManager.gotoPage(PMGame.PAGE_MENU);
			// create FPS Counter
			// mCanvas.addChild(new FPSCounter(0 , 0));
			
			
			trace( "Global.dataGame.levels.length : " + Global.dataGame.levels.length );
		}
		
		// ---------------------
		// create object Project
		// ---------------------
		public static function initialize(canvas:MovieClip):void {
			Global.project = new ModProject(canvas);
            // guardando la referencia en la clase Project
            Project.instance = Global.project;
		}
		// ----------------------------
		// static function after create
		// ----------------------------
		public static function getCanvas():MovieClip {
			return Global.project.canvas;
		}
		
		public static function addTask(exec:Function , delay:int):void {
			Global.project.addTask(exec , delay);
		}
		
		public static function onBuild():void {
			Global.project.onBuild();
		}
		
		public static function getInstance():ModProject {
			return Global.project;
		}		
	}

}